<?php
// +----------------------------------------------------------------------
// | 十分彩网系统 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Common\Controller\HomebaseController; 
use Wechat\Controller\OrderController;
/**
 * 首页
 */
class PayController extends HomebaseController {
	public function payOrderCreate($money=array()){
		$vars['type'] = I('get.content_type');//支付类型
		$vars['paytype'] = I('get.paytype');
		$vars['packid']  = I('post.packid');
		$vars['order_sn']  = $money['order_sn'];
		if(sp_is_user_login()){
			if($vars['type']==''){
				$this->error("您的支付没有标明是什么类型，请找管理员进行查证");
			}
			if($vars['type']=='0'){//订单支付
				
			}elseif($vars['type']=='1'){//账户充值
				$user = session('user');
				$pay_arr=array('mid'=>$user['agentid'],'muser'=>$user['a_name']);
				$pay_arr["content_money"]=$money['money'];
				$orderrecord = new OrderController();
				$orders=$orderrecord->addPayRecord($vars,$pay_arr);
				return $orders;
			}
		}else{
			$this->error("请登录！",U("user/login/index"));
		}
		
	}
	public function notifyurl(){
		vendor('wxpay.WxPayPubHelper.WxPayPubHelper');
		vendor('wxpay.log_');
		$notify = new \Notify_pub();
		//存储微信的回调
		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$notify->saveData($xml);
		if($notify->checkSign() == FALSE){
			$notify->setReturnParameter("return_code","FAIL");//返回状态码
			$notify->setReturnParameter("return_msg","签名失败");//返回信息
		}else{
			$notify->setReturnParameter("return_code","SUCCESS");//设置返回码
		}
		$returnXml = $notify->returnXml();
		$log_ = new \Log_();
		$log_name=VENDOR_PATH."wxpay/notify_url.log";//log文件路径
		$log_->log_result($log_name,"【接收到的notify通知】:\n".$xml."\n");
		if($notify->checkSign() == TRUE)
		{
			if ($notify->data["return_code"] == "FAIL") {
				//此处应该更新一下订单状态，商户自行增删操作
				$log_->log_result($log_name,"【通信出错】:\n".$xml."\n");
			}
			elseif($notify->data["result_code"] == "FAIL"){
				//此处应该更新一下订单状态，商户自行增删操作
				$log_->log_result($log_name,"【业务出错】:\n".$xml."\n");
			}
			else{
				//此处应该更新一下订单状态，商户自行增删操作
				$log_->log_result($log_name,"【支付成功】:\n".$xml."\n");
				$out_trade_no=$notify->data["out_trade_no"];
				$total_fee=$notify->data["total_fee"]/100;
				$transaction_id=$notify->data["transaction_id"];//微信支付订单号		
				$this->PaySuccessReturn($out_trade_no,$total_fee,1,$transaction_id);	
			}
		}
	}
	public function PaySuccessReturn($dingdan,$total,$paytype,$trade_no){
		$order = new OrderController();
		$status=$order->PaySuccessReturn($dingdan,$total,$paytype,$trade_no);
		if($status==true){
			$this->success("支付成功",U('user/center/index'));
		}else{
			$this->error("支付失败",U('user/center/index'));
		}
	}
}
?>